package cn.caplike.demo.repository.java.concurrency.threadpool;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 认识Callable，对Runnable进行了扩展
 */
public class T03_CallableFuture {

    public static void main(String[] args) {
        Callable<Integer> callable = new Callable<Integer>() {
            public Integer call() throws Exception {
                return new Random().nextInt(100);
            }
        };
        FutureTask<Integer> future = new FutureTask<>(callable);
        new Thread(future).start();
        System.out.println("FutureTask 启动...");
        try {
            System.out.println("去完成其他工作");
            Thread.sleep(5000);// 可能做一些事情
            System.out.println("5s 后获取 FutureTask 结果: " + future.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }

}
